package com.lhj.jpa.future;

import java.util.concurrent.CompletableFuture;

/**
 * @author linghaijin781
 * @date 2021/9/2210:41 下午
 */
public class _02_exceptionally {
	public static void main(String[] args) {
		SmallTool.printTimeAndThread("小白打算坐公交回家");
		SmallTool.printTimeAndThread("797路 和 706路都可以回家");

		CompletableFuture<String> bus = CompletableFuture.supplyAsync(() -> {
			SmallTool.printTimeAndThread("797路公交车即将进站");
			SmallTool.sleepMills(300);
			return "797路公交车进站";
		}).applyToEither(CompletableFuture.supplyAsync(() -> {
			SmallTool.printTimeAndThread("706路公交车即将进站");
			SmallTool.sleepMills(200);
			return "706路公交车进站";
		}), firstComeBus -> {
			SmallTool.printTimeAndThread(firstComeBus);
			if (firstComeBus.startsWith("797")) {
				throw new RuntimeException("公交车爆胎了");
			}
			return firstComeBus;
		}).exceptionally(e -> {
			SmallTool.printTimeAndThread(e.getMessage());
			SmallTool.printTimeAndThread("小白叫了滴滴");
			return "滴滴，快车到了";
		});

		SmallTool.printTimeAndThread(String.format("%s, 小白上车回家", bus.join()));
	}
}
